package com.yq.controller.system;

import com.yq.constants.Constants;
import com.yq.constants.HttpStatus;
import com.yq.domain.Menu;
import com.yq.domain.SimpleUser;
import com.yq.dto.LoginBodyDto;
import com.yq.servicce.MenuService;
import com.yq.vo.ActiveUser;
import com.yq.vo.AjaxResult;
import com.yq.vo.MenuTreeVo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yangQ
 * @Date: 2020/11/24 11:25
 * @Description:后台登录相关接口
 */
@RestController
@RequestMapping("login")
@Log4j2
public class LoginController {

    @Autowired
    private MenuService menuService;

    @PostMapping("doLogin")
    public AjaxResult login(@RequestBody @Validated LoginBodyDto loginBodyDto){
        String username = loginBodyDto.getUsername();
        String password = loginBodyDto.getPassword();
        AjaxResult ajaxResult = AjaxResult.success();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(token);
            Serializable webToken = subject.getSession().getId();
            ajaxResult.put(Constants.TOKEN,webToken);
        }catch (Exception e){
            log.error("用户名或密码不正确", e);
            ajaxResult = AjaxResult.error(HttpStatus.ERROR, "用户名或密码不正确");
        }
        return ajaxResult;
    }

    /**
     * 获取用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo(){
        Subject subject = SecurityUtils.getSubject();
        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
        AjaxResult ajaxResult =AjaxResult.success();
        ajaxResult.put("username",activeUser.getUser().getUserName());
        ajaxResult.put("picture",activeUser.getUser().getPicture());
        ajaxResult.put("roles",activeUser.getRoles());
        ajaxResult.put("permission",activeUser.getPermissions());
        return ajaxResult;
    }

    /**
     * 退出用户
     */
    @PostMapping("logout")
    public AjaxResult logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return AjaxResult.success("用户退出成功");
    }

    /**
     * 获取菜单数据
     */
    @GetMapping("getMenus")
    public AjaxResult getMenus(){
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        boolean isAdmin = activeUser.getUser().getUserType().equals(Constants.USER_ADMIN);
        SimpleUser simpleUser=null;
        if (!isAdmin){
            simpleUser =new SimpleUser(activeUser.getUser().getUserId(),activeUser.getUser().getUserName());
        }
        List<Menu> menus = menuService.selectMenuTree(isAdmin,simpleUser);
        List<MenuTreeVo> menuVos=new ArrayList<>();
        for (Menu menu : menus) {
            menuVos.add(new MenuTreeVo(menu.getMenuId().toString(),menu.getPath()));
        }
        return AjaxResult.success(menuVos);
    }
}
